//
//  XRCenterViewController.m
//  XDLookPic
//
//  Created by xiaorui on 14-4-21.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "XRCenterViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface XRCenterViewController ()

@end

@implementation XRCenterViewController
@synthesize scontenView = _scontentView;
@synthesize tabBarView = _tabBarView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationBarView.hidden =NO;
    viewControllers =[[NSMutableArray alloc]initWithCapacity:0];
    FirstViewController * vc1 = [[FirstViewController alloc] init];
    SecondViewController * vc2 = [[SecondViewController alloc] init];
    ThirdViewController * vc3 = [[ThirdViewController alloc] init];
    [viewControllers addObjectsFromArray:@[vc1,vc2,vc3]];
    _scontentView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT -UI_TAB_BAR_HEIGHT)];
    _scontentView.backgroundColor =[UIColor clearColor];
    [self.view addSubview:_scontentView];
    
    _tabBarView =[[UIView alloc]initWithFrame:CGRectMake(0, UI_SCREEN_HEIGHT -UI_TAB_BAR_HEIGHT, UI_SCREEN_WIDTH, UI_TAB_BAR_HEIGHT)];
    _tabBarView.backgroundColor =[UIColor clearColor];
    [self.view addSubview:_tabBarView];
    [_scontentView addSubview:vc1.view];
    [self addChildViewController:vc1];
    [self creatBUttonItem];
}
-(void)creatBUttonItem{
    for(int  i= 0;i<viewControllers.count;i++){
        UIButton *button = creatXRButton(CGRectMake(i*UI_SCREEN_WIDTH/TABBARCOUNT,0,UI_SCREEN_WIDTH/TABBARCOUNT,UI_TAB_BAR_HEIGHT),  nil, [UIImage imageNamed:[NSString stringWithFormat:@"tab_button%d_notOn",i+1]] , [UIImage imageNamed:[NSString stringWithFormat:@"tab_button%d_on",i+1]]);
        button.tag =UIBUTTON_TAG +i;
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_button%d_on",i+1]] forState:UIControlStateDisabled];
        [button addTarget:self action:@selector(chooseTabBar:) forControlEvents:UIControlEventTouchDown];
		[_tabBarView addSubview:button];
    }
}
-(void)chooseTabBar:(UIButton *)sender{
    [self setViewControllerView:sender.tag - UIBUTTON_TAG];
}
#pragma mark  设置界面现实的ViewController的view
-(void)setViewControllerView:(NSInteger)sender{
    [[_scontentView subviews]makeObjectsPerformSelector:@selector(removeFromSuperview)];
     UIViewController *vc=[viewControllers objectAtIndex:sender];
    if(sender == 1){
        
        [self presentViewController:vc animated:YES completion:^{
            [self setViewControllerView:sender - 1];
        }];
    }else{
        [_scontentView addSubview:vc.view];
        [self addChildViewController:vc];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
